// pages/msg/msg.js
import config from "../../config.js";
const base = config;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 我的管家
    manager: {
      star: []
    },

    // 消息列表
    msg: [{
        icon: "../../image/default.png",
        title: "菇咚公告",
        detail: "成交啦！您提交的接送机服务已完成啦！感谢您的信任您提交的接送机服务已完成啦您提交的接送机服务已完成啦！感谢您的信任！",
        time: "12:30",
      }, {
        icon: "../../image/default.png",
        title: "活动消息",
        detail: "成交啦！您提交的接送机服务已完成啦！感谢您的信任！",
        time: "12:30",
      },
      {
        icon: "../../image/default.png",
        title: "私信消息",
        detail: "成交啦！您提交的接送机服务已完成啦！感谢您的信任！",
        time: "12:30",
      }
    ],

    //订单助手
    orderHelper: {},
    //菇咚公告
    gdNotice: {},
    //活动公告
    actNotice: {},
    //即时消息
    gdMessage: []
  },
  changeKeeper: function() {
    wx.navigateTo({
      url: '../managerList/managerList?isChangeDefault=1',
    })

  },
  makeCall: function(e) {
    let tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },

  orderAction: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './msgList?id=' + id,
    })
  },

  gdAction: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './msgList?id=' + id,
    })
  },

  actAction: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './msgList?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.showLoading({
      title: '请稍等',
    })

    // 专属管家
    wx.request({
      url: base.myHouseKepper,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        id: wx.getStorageSync("user").housekeeper.keeperId,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 200) {
          var star = [];
          for (var i = 0; i < res.data.data.score; i++) {
            star.push(i);
          }
          that.setData({
            manager: res.data.data,
            'manager.star': star,
          })
        }
      }
    })


    //订单助手最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 1,
        typeId: 1,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              orderHelper: obj
            })
          } else {
            let arr = [];
            that.setData({
              orderHelper: res.data.data[0]
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })

    //菇咚公告最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 1,
        typeId: 2,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              gdNotice: obj
            })
          } else {
            let arr = [];
            that.setData({
              gdNotice: res.data.data[0]
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })

    //活动消息最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 1,
        typeId: 3,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              actNotice: obj
            })
          } else {
            let arr = [];
            that.setData({
              actNotice: res.data.data[0]
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var keeper = wx.getStorageSync("MyDefaultHouseKeeper");
    var star = [];
    for (var i = 0; i < keeper.score; i++) {
      star.push(i);
    }
    if (keeper != null && keeper != '') {
      this.setData({
        manager: keeper,
        'manager.star': star
      })
    }

    that.loadOffData();

    that.addListener();

  },
  addListener: function() {
    var that = this;
    console.log("添加监听");
    app.globalData.callback = function(res) {
      console.log('收到服务器内容3：' + res.data)
      wx.showTabBarRedDot({
        index: 1,
      })
      that.loadOffData();
    }

  },

  loadOffData: function() {
    var that = this;
    //即时通讯
    wx.request({
      url: base.chatOffLineMessageList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        start: 0,
        size: 10,
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {

            that.setData({
              gdMessage: []
            })
          } else {
            let arr = [];
            that.setData({
              gdMessage: res.data.data
            })


            wx.showTabBarRedDot({
              index: 1,
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  contactAction: function() {
    var that = this;
    //请求车辆列表
    wx.request({
      url: base.defaultKeeperAccount,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 200) {
          wx.setStorageSync("keeperAccount", res.data.data);
          wx.navigateTo({
            url: '../msg/chat?type=2',
          })

        } else {
          wx.showToast({
            title: '即时通讯信息获取失败',
            icon: 'none'
          })
        }
      }
    })

  },
  messageAction: function() {
    wx.hideTabBarRedDot({
      index: 1,
    })
    wx.navigateTo({
      url: '../gjIndex/msgList/msgList?type=1',
    })
  }
})